package model

import (
	"context"
	"database/sql"
	"fmt"
	"github.com/zeromicro/go-zero/core/stores/cache"
	"github.com/zeromicro/go-zero/core/stores/sqlc"
	"github.com/zeromicro/go-zero/core/stores/sqlx"
)

var _ WalletModel = (*customWalletModel)(nil)

type (
	// WalletModel is an interface to be customized, add more methods here,
	// and implement the added methods in customWalletModel.
	WalletModel interface {
		walletModel
		Trans(ctx context.Context, fn func(context context.Context, session sqlx.Session) error) error
		FindOneBySourceId(ctx context.Context, id int64) (*Wallet, error)
		TransInsert(ctx context.Context, session sqlx.Session, data *Wallet) (sql.Result, error)
		TransUpdate(ctx context.Context, session sqlx.Session, data *Wallet) error
	}

	customWalletModel struct {
		*defaultWalletModel
	}
)

// NewWalletModel returns a model for the database table.
func NewWalletModel(conn sqlx.SqlConn, c cache.CacheConf) WalletModel {
	return &customWalletModel{
		defaultWalletModel: newWalletModel(conn, c),
	}
}

func (m *defaultWalletModel) Trans(ctx context.Context, fn func(ctx context.Context, session sqlx.Session) error) error {

	return m.TransactCtx(ctx, func(ctx context.Context, session sqlx.Session) error {
		return fn(ctx, session)
	})

}

func (m *defaultWalletModel) FindOneBySourceId(ctx context.Context, id int64) (*Wallet, error) {
	publicWalletSourceIdKey := fmt.Sprintf("%s%v", cachePublicWalletSourceIdPrefix, id)
	var resp Wallet
	err := m.QueryRowCtx(ctx, &resp, publicWalletSourceIdKey, func(ctx context.Context, conn sqlx.SqlConn, v interface{}) error {
		query := fmt.Sprintf("select %s from %s where source_id = $1 limit 1", walletRows, m.table)
		return conn.QueryRowCtx(ctx, v, query, id)
	})
	switch err {
	case nil:
		return &resp, nil
	case sqlc.ErrNotFound:
		return nil, ErrNotFound
	default:
		return nil, err
	}
}

func (m *defaultWalletModel) TransInsert(ctx context.Context, session sqlx.Session, data *Wallet) (sql.Result, error) {
	publicWalletIdKey := fmt.Sprintf("%s%v", cachePublicWalletIdPrefix, data.Id)
	publicWalletSourceIdKey := fmt.Sprintf("%s%v", cachePublicWalletSourceIdPrefix, data.SourceId)
	ret, err := m.ExecCtx(ctx, func(ctx context.Context, conn sqlx.SqlConn) (result sql.Result, err error) {
		query := fmt.Sprintf("insert into %s (%s) values ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11, $12)", m.table, walletRowsExpectAutoSet)
		if session != nil {
			return session.ExecCtx(ctx, query, data.Id, data.SourceType, data.SourceId, data.Balance, data.Status, data.BankName, data.BankNumber, data.CreateTime, data.UpdateTime, data.SaleCardIncome, data.CouponIncome, data.AccountName)
		}
		return conn.ExecCtx(ctx, query, data.Id, data.SourceType, data.SourceId, data.Balance, data.Status, data.BankName, data.BankNumber, data.CreateTime, data.UpdateTime, data.SaleCardIncome, data.CouponIncome, data.AccountName)
	}, publicWalletIdKey, publicWalletSourceIdKey)
	return ret, err
}

func (m *defaultWalletModel) TransUpdate(ctx context.Context, session sqlx.Session, data *Wallet) error {
	publicWalletIdKey := fmt.Sprintf("%s%v", cachePublicWalletIdPrefix, data.Id)
	publicWalletSourceIdKey := fmt.Sprintf("%s%v", cachePublicWalletSourceIdPrefix, data.SourceId)
	_, err := m.ExecCtx(ctx, func(ctx context.Context, conn sqlx.SqlConn) (result sql.Result, err error) {
		query := fmt.Sprintf("update %s set %s where id = $1", m.table, walletRowsWithPlaceHolder)
		if session != nil {
			return session.ExecCtx(ctx, query, data.Id, data.SourceType, data.SourceId, data.Balance, data.Status, data.BankName, data.BankNumber, data.CreateTime, data.UpdateTime, data.SaleCardIncome, data.CouponIncome, data.AccountName)
		}
		return conn.ExecCtx(ctx, query, data.Id, data.SourceType, data.SourceId, data.Balance, data.Status, data.BankName, data.BankNumber, data.CreateTime, data.UpdateTime, data.SaleCardIncome, data.CouponIncome, data.AccountName)
	}, publicWalletIdKey, publicWalletSourceIdKey)
	return err
}
